package com.campusflea.fragment;

/**
 * Created by Administrator on 2017/6/23.
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.campusflea.R;
import com.campusflea.person.DataModel;
import com.campusflea.person.TypeAbstractViewHolder;
import com.campusflea.person.TypeTwoViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
/** * Created by wnw on 16-5-20. */
public class MyAdapter_shop extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_HEADER = 0;  //说明是带有Header的
public static final int TYPE_FOOTER = 1;  //说明是带有Footer的
public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

//获取从Activity中传递过来每个item的数据集合
//HeaderView, FooterView
    private View mHeaderView;
    private View mFooterView;
    private List<DataModel> mlist=new ArrayList<>();
    private Context mContext;
    private LayoutInflater mLayoutInfater;

//构造函数
public MyAdapter_shop(List<DataModel> list,Context context){
        this.mlist = list;
    this.mContext=context;
    mLayoutInfater=LayoutInflater.from(context);
    }

public void addList(List<DataModel> list)
{
        mlist.addAll(list);
}
//HeaderView和FooterView的get和set函数
public View getHeaderView() {
        return mHeaderView;
        }
public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
        }
public View getFooterView() {
        return mFooterView;
        }
public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
        }

/** 重写这个方法，很重要，是加入Header和Footer的关键，我们通过判断item的类型，从而绑定不同的view    * */
@Override
public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null){
        return TYPE_NORMAL;
        }
        if (position == 0){
        //第一个item应该加载Header
        return TYPE_HEADER;
        }
        if (position == getItemCount()-1){
        //最后一个,应该加载Footer
        return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
        }

//创建View，如果是HeaderView或者是FooterView，直接在Holder中返回
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER) {
        return new ListHolder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER){
        return new ListHolder(mFooterView);
        }

        return new TypeTwoViewHolder(mLayoutInfater.inflate(R.layout.item_type_two,parent,false));
        }

//绑定View，这里是根据返回的这个position的类型，从而进行绑定的，   HeaderView和FooterView, 就不同绑定了
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    Picasso.with(mContext).load(mlist.get(position).url).resize(90,90).into(((TypeAbstractViewHolder) holder).avatar);
        if(getItemViewType(position) == TYPE_NORMAL){
        if(holder instanceof ListHolder) {
        //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
            ((TypeAbstractViewHolder)holder).binHolder(mlist.get(position));
            return;
        }
        return;
        }else if(getItemViewType(position) == TYPE_HEADER){

        return;
        }else{
        return;
        }
        }

//在这里面加载ListView中的每个item的布局
class ListHolder extends RecyclerView.ViewHolder{

    private ImageView avatar;
    private TextView name;
    private TextView price;
    public TextView content;
    public ListHolder(View itemView) {
        super(itemView);
        //如果是headerview或者是footerview,直接返回
        if (itemView == mHeaderView){
            return;
        }
        if (itemView == mFooterView){
            return;
        }
        avatar= (ImageView) itemView.findViewById(R.id.avatar);
        name= (TextView) itemView.findViewById(R.id.name);
        price= (TextView) itemView.findViewById(R.id.price);
        content= (TextView) itemView.findViewById(R.id.content);
    }
}

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        if(mHeaderView == null && mFooterView == null){
            return mlist.size();
        }else if(mHeaderView == null && mFooterView != null){
            return mlist.size() + 1;
        }else if (mHeaderView != null && mFooterView == null){
            return mlist.size() + 1;
        }else {
            return mlist.size() + 2;
        }
    }
}